<!-- 列表项 :供应 -->
<template>
<div class="item hand " @click="publishByMe?go2DemandEditDetail(item):go2DemandDetail(item)">
    <div class="tag tag2">{{$t('demand-supply')}}</div>
    <div class="item_left">
        <div class="avatarBox" v-if="!publishByMe">
        	<div class="avatar">
        		<img :src="item.avatar?item.avatar:avatar" alt="" class="img">
        	</div>
        	<div>
        		<p class="name">{{item.nickname}}</p>
        	</div>
        </div>
        <div class="avatarBox" v-else>
        	<div class="avatar">
        		<img :src="userInfo.avatar" alt="" class="img">
        	</div>
        	<div>
        		<p class="name">{{userInfo.nickname}}</p>
        	</div>
        </div>
    </div>
    <div class="item_cen">
        <div class="item_cen_l">
            <p class="item_cen_name mb">{{item.title}} <img src="@/assets/img/icon_new.svg" alt="" v-if="isNew(item.createTime)" class="img"></p>
            <!-- 品牌 -->
            <p class="item_region">{{$t('demand-label-brandReq')}}{{item.brandName}}</p>
            <!-- 公司名称 -->
            <p class="item_txt">{{$t('demand-detail-company')}}：{{item.companyName}}1</p>
            <!-- 发布时间 -->
            <p class="item_txt">{{$t('demand-label-createTime')}}{{item.createTime}}</p>
        </div>
        <div class="item_cen_r">
            <p class="item_num">{{$t('demand-label-production-supply')}}{{item.outputNum}}</p>
        </div>
    </div>
    <div class="item_right">
		<template v-if="!item.isMe && !fromCenter">
        <span class="btn btn-disabled" v-if="isCollect">{{$t('claimed')}}</span>
        <span class="btn hand" @click.stop="getCollect(item)" v-else>{{$t('claim')}}</span>
		</template>
    </div>
</div>
</template>

<script>
import {
    getCollect,
} from '@/api/demand'
export default {
    props: {
        item: {
            type: Object,
            required: true
        },
		//是否来自个人中心，隐藏立即认领按钮
		fromCenter: {
			type: Boolean,
			default: false
			
		},
		//是否来自个人中心我的发布，展示我的头像昵称
		publishByMe: {
			type: Boolean,
			default: false
			
		},
    },

    computed: {
        isNew() {
            return (date) => {
                const days = 7 // 相差的天数小于这个天数为最新
                if (!date) return
                let startTime = new Date(date); // 创建时间
                let currentTime = new Date(); // 当前时间
                return (currentTime - startTime) / 1000 / 60 / 60 / 24 < days;
            }
        },
    },
    data() {
        return {
            isCollect:0,
			avatar: require('assets/img/logo.svg')
        }
    },
    mounted() {
        this.isCollect=this.item?this.item.isCollect:0;
    },
    methods: {
        // 领取
        getCollect(item) {
            if (!this.userInfo) {
                this.$modal.confirm(this.$t("tip-login"),this.locale).then(() =>{
                    this.go2Login()
                })
                return;
            }
            this.invoke(getCollect, item.id).then(res => {
                if (res && res.code == 200) {
                    this.$message.success(this.$t('claim-success'));
                    this.isCollect=1;
                }
            })
        },
	
    }
}
</script>

<style lang="scss" scoped src="@/assets/styles/demand-item.scss"></style>
